package com.cs5103.calendar;

import java.text.DateFormatSymbols;
import java.util.ArrayList;
import java.util.Calendar;

import android.content.Context;
import android.widget.Toast;

// SINGLETON for application data availability across the application

public class CalendarData {
	private static CalendarData sCalendar;
	private Context mAppContext;
//	private ArrayList<CalendarCell> mCells;
	private Calendar mDateToday;
	private Calendar mDateSelected;
	private Calendar mDateCurrentView;
//	private String mCalendarHeader;
	private String[] mDaysOfWeek_Abbr;
	private String[] mDaysOfWeek_Full;
	private String[] mMonths;
	private int mCellExtent;
	
	private static final int CELLS_IN_MONTH = 42;
	private static final int CELLS_IN_WEEK = 7;
	private static final int CELLS_IN_DAY = 1;
		
	private CalendarData(Context appContext) {
		mAppContext = appContext;
		mCellExtent = 42;
		mDateSelected = Calendar.getInstance();
		mDateCurrentView = Calendar.getInstance();
		initializeDaysOfWeek();
		initializeMonths();
//		initializeCells(mDateCurrentView);
///		initializeHeader(mDateSelected);
	}
	
	public static CalendarData get(Context c) {
		if (sCalendar == null)
			sCalendar = new CalendarData(c.getApplicationContext());
		
		return sCalendar;
	}
	
//	public ArrayList<CalendarCell> getCells() {
//		return mCells;
//	}
	
	public Calendar getToday() {
		mDateToday = Calendar.getInstance();
		return mDateToday;
	}
	
	public void setDateCurrentView(Calendar mDate) {
		mDateCurrentView = (Calendar)mDate.clone();
//		initializeCells(mDateCurrentView);
//		initializeHeader(mDateSelected);
	}
	
	public Calendar getDateCurrentView() {
		return mDateCurrentView;
	}
	
	public void setDateSelected(Calendar mDate) {
		mDateSelected = (Calendar) mDate.clone();
		mDateCurrentView = (Calendar)mDate.clone();
//		initializeCells(mDateCurrentView);
//		initializeHeader(mDateCurrentView);
	}
	
	public Calendar getDateSelected() {
		return mDateSelected;
	}
	
	public int getCellExtent() {
		return mCellExtent;
	}
	
//	public String getCalendarHeader() {
//		return mCalendarHeader;
//	}
	
	/* Initialize array list of days of week string and 
	 * substring of abbreviated days of week.
	 * Convert 1 based days of week array list to zero based
	 * days of week abbreviated days of week.
	 */
	private void initializeDaysOfWeek() {
		mDaysOfWeek_Full = new DateFormatSymbols().getWeekdays();
		mDaysOfWeek_Abbr = new String[mDaysOfWeek_Full.length - 1];
		String mTemp;
		
		for (int i = 1; i < mDaysOfWeek_Abbr.length; i++) {
			mTemp = mDaysOfWeek_Full[i];
			mDaysOfWeek_Full[i-1] = mTemp;
			mDaysOfWeek_Abbr[i-1] = mTemp.substring(0,3);
		}
			
	}
	
	public String[] getDaysOfWeek_Abbr() {
		return mDaysOfWeek_Abbr;
	}
	
	private void initializeMonths() {
		mMonths = new DateFormatSymbols().getMonths();
	}
	
	public String[] getMonths() {
		return mMonths;
	}
	
//	public int interpolateCalendarGrid() {
//		return (Calendar.getInstance().get(Calendar.YEAR) - 1900) * 12;
//	}
	
	/* Calendar APP navigation functions
	 * adjust calendar display (Month, Week, Year)
	 */
	
	public void setCellExtent(int mNavigationPosition) {
		switch (mNavigationPosition) {
			case 0: {
				mCellExtent = CELLS_IN_MONTH;
				break;
			}
			case 1: {
				mCellExtent = CELLS_IN_WEEK;
				break;
			}
			case 2: {
				mCellExtent = CELLS_IN_DAY;
				break;
			}
		}
		setDateSelected(mDateSelected); // Date not changed but layout has - update the view layout
	}
	
	public void incrementDateSelected() {
		switch (mCellExtent) {
			case CELLS_IN_MONTH: {
				mDateSelected.add(Calendar.MONTH, 1);
				break;
			}
			case CELLS_IN_WEEK: {
				mDateSelected.add(Calendar.WEEK_OF_YEAR, 1);
			}
		}
		setDateSelected(mDateSelected); // Date selected incremented by scroll - update the view data
	}
}
